package cn.appsys.controller.backend;

import cn.appsys.pojo.DevUser;
import cn.appsys.service.developer.DevUserService;
import cn.appsys.tools.Constants;
import cn.appsys.tools.PageSupport;
import cn.appsys.tools.ReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/manager/backend/audit")
public class UserAuditController {
    @Autowired
    private DevUserService devUserService;
    @RequestMapping("/useraudit")
    public String toUserManagement(String devName, String pageIndex, Model model) {
        System.out.println("============================toUserManagement============================");
        //这是pagesize
        int pageSize = Constants.pageSize;
        //设置当前页码
        Integer currentPageNo = 1;
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        //封装参数
        Map<String, Object> map = new HashMap<>();
        map.put("beginNo", (currentPageNo - 1) * pageSize);
        map.put("pageSize", pageSize);
        map.put("devName", devName);
        //总记录数
        int totalCount = 0;
        try {
            totalCount = devUserService.queryDevUserCountByCondition(map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //校验页码
        if (currentPageNo < 1) {
            currentPageNo = 1;
        } else if (currentPageNo > totalPageCount) {
            currentPageNo = totalPageCount;
        }
        List<DevUser> devUserList = null;
        try {
            devUserList = devUserService.queryDevUserByConditionForPage(map);
        } catch (Exception e) {
            e.printStackTrace();
        }

        model.addAttribute("devUserList", devUserList);
        model.addAttribute("pages", pages);
        model.addAttribute("devName", devName);
        return "backend/useraudit";
    }


    @RequestMapping("/audit")
    @ResponseBody
    public Object audit(DevUser devUser){
        ReturnObject returnObject = new ReturnObject();
        try {
            int ret = devUserService.editDevUserById(devUser);
            if (ret>0){
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_SUCCESS);
            }else {
                returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
                returnObject.setMessage("操作失败，请稍后再试！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnObject.setCode(Constants.RETURN_OBJECT_CODE_FAIL);
            returnObject.setMessage("操作失败，请稍后再试！");
        }
        return returnObject;
    }
}
